#!/bin/bash

#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <mj@casalogic.dk> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return Martin Juhl
# ----------------------------------------------------------------------------
#

#    This file is part of bumblebee.
#
#    bumblebee is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    bumblebee is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with bumblebee.  If not, see <http://www.gnu.org/licenses/>.
#


ROOT_UID=0

if [ `cat /etc/issue | grep -nir fedora | wc -l` -gt 0 ] ; then
 DISTRO=FEDORA
elif [ `cat /etc/issue | grep -nir ubuntu | wc -l` -gt 0 ] ; then
 DISTRO=UBUNTU
elif [ `cat /etc/issue | grep -nir opensuse | wc -l` -gt 0 ] ; then
 DISTRO=OPENSUSE
 if [ "$SUDO_USER" != "" ] ; then
  HOME=`cat /etc/passwd | grep $SUDO_USER | cut -f6 -d':'`
 fi
elif [ `cat /etc/issue | grep -nir debian | wc -l` -gt 0 ] ; then
 DISTRO=DEBIAN
fi

if [ "$UID" != "$ROOT_UID" ] ; then
    echo "You don't have sufficient privileges to run this script."
    echo
    echo "Please run the script with: sudo bumblebee-bugreport"
    exit 1
fi
  
if [ "$HOME" = "/root" ] && [ "$DISTRO" != OPENSUSE ] ; then
    echo "Do not run this script as the root user"
    echo
    echo "Please run the script with: sudo bumblebee-bugreport"
    exit 2
fi

echo
echo "Bug report is being generated"
echo

rm -rf /tmp/bug-report
mkdir -p /tmp/bug-report

cp /var/log/Xorg.* /tmp/bug-report/
cp /etc/X11/xorg.conf* /tmp/bug-report/
cp /var/log/bumblebee.* /tmp/bug-report/
cp /etc/bash.bashrc /tmp/bug-report/


echo "Bumblebee Bug Report" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report

echo `cat /etc/bumblebee` >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report

echo `cat /etc/issue` >> /tmp/bug-report/bumblebee-report

echo "uname -a info" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
uname -a >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report

echo "lspci info" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
lspci >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report

echo "ps aux info" >> /tmp/bug-report/bumblebee-report
ps aux |grep X >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report

echo "library info" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
echo "/usr/lib32/nvidia-current/" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
if [ -d /usr/lib32/nvidia-current ]; then
 ls -la /usr/lib32/nvidia-current >> /tmp/bug-report/bumblebee-report
fi
echo >> /tmp/bug-report/bumblebee-report
echo "/usr/lib64/nvidia-current/" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
if [ -d /usr/lib64/nvidia-current ]; then
 ls -la /usr/lib64/nvidia-current >> /tmp/bug-report/bumblebee-report
fi >> /tmp/bug-report/bumblebee-report

echo "/usr/lib/nvidia-current/" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
if [ -d /usr/lib/nvidia-current ]; then
 ls -la /usr/lib/nvidia-current >> /tmp/bug-report/bumblebee-report
fi >> /tmp/bug-report/bumblebee-report
 

echo >> /tmp/bug-report/bumblebee-report
echo "modules info" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
lsmod >> /tmp/bug-report/bumblebee-report

echo >> /tmp/bug-report/bumblebee-report
echo "alternatives info" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
ls -la /etc/alternatives >> /tmp/bug-report/bumblebee-report

echo >> /tmp/bug-report/bumblebee-report
echo "/etc/default/bumblebee" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
cat /etc/default/bumblebee >> /tmp/bug-report/bumblebee-report

echo >> /tmp/bug-report/bumblebee-report
echo "Enable/Disable Scripts" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report

cat /usr/local/bin/bumblebee-enablecard >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
cat /usr/local/bin/bumblebee-disablecard >> /tmp/bug-report/bumblebee-report

echo >> /tmp/bug-report/bumblebee-report
echo "dmidecode" >> /tmp/bug-report/bumblebee-report
echo >> /tmp/bug-report/bumblebee-report
dmidecode --string baseboard-manufacturer >> /tmp/bug-report/bumblebee-report
dmidecode --string baseboard-product-name >> /tmp/bug-report/bumblebee-report
dmidecode --string system-manufacturer >> /tmp/bug-report/bumblebee-report
dmidecode --string system-product-name >> /tmp/bug-report/bumblebee-report

tar zcf $HOME/bumblebee-bugreport-`date +2%3y%m%d_%H%M%S`.tar.gz /tmp/bug-report/

echo "Bug report generated"
echo
echo "The bug report has been saved as "$HOME/bumblebee-bugreport-`date +2%3y%m%d_%H%M%S`.tar.gz
echo "Please create a bugreport on github and email me the bug report along with the"
echo "issue number to mj@casalogic.dk"
echo
echo "https://github.com/MrMEEE/bumblebee/issues"
echo 
echo "This really makes bugfixing much easier for me and faster for you"
echo
echo "Martin Juhl"
